// pages/coupon/coupon.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: 0,
    shoplist:'',
    bnyr:'',
    enyr:'',
  },
  // 立即使用
  tiaozhuans:function(e){
    var idx= e.currentTarget.dataset.index

    var id = e.currentTarget.dataset.id    //发布方ID
    var aid = e.currentTarget.dataset.aid   //活动ID

    if (this.data.shoplist[idx].CouponType == 0){
      wx.navigateTo({
        url: '/pages/index/index',
      })
    } else if (this.data.shoplist[idx].CouponType == 3) {  //商品详情  传id   
        wx.navigateTo({
          url: '/pages/supermarket-details/details?ID=' + aid + '&shopID=' + id,
        })
    } else if (this.data.shoplist[idx].CouponType == 2) { //跳转商户 传商户id  PublisherID
      wx.navigateTo({
        url: '/pages/shop-homepage/homepage?ID=' + id,
      })
    }
    
    
    
    
    else if (this.data.shoplist[idx].CouponType == 1) { //拼购详情 传id   ActivityID
        // wx.navigateTo({
        //   url: '/pages/group-purchase-commodity/commodity?ID=' + aid,
        // })
      wx.showToast({
        title: '拼购暂未开启',
        icon: 'none'
      })
    } 
  },
  tiaozhuan:function(){
    wx.navigateTo({
      url: '/pages/securitiescentre/securitiescentre',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    // this.getone()
  },
  selecttabone:function(e){
    var select = e.currentTarget.dataset.index;
    this.setData({
      select: select
    })
    this.getone();
  },
  getone:function(){
    ajax('/api/ReceiveCoupon/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 1,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        this.setData({
          shoplist: res.Models
        })
      })
  },
  selecttabtwo: function (e) {
    var select = e.currentTarget.dataset.index;
    this.setData({
      select: select,
      shoplist:""
    })
    ajax('/api/ReceiveCoupon/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 2,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        this.setData({
          shoplist: res.Models
        })
        for(var a in this.data.shoplist){
          this.setData({
            bnyr: this.data.shoplist[a].Bdate.slice(0,10),
            enyr: this.data.shoplist[a].Edate.slice(0, 10)
          })
        }
      })
  },
  selecttabthree: function (e) {
    var select = e.currentTarget.dataset.index;
    this.setData({
      select: select,
      shoplist: ""
    })
    ajax('/api/ReceiveCoupon/Get?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      CheckFlag: 3,
      PageIndex: 0,
      ProvinceName: wx.getStorageSync('address_component').province,
      CityName: wx.getStorageSync('address_component').city,
      DistrictName: wx.getStorageSync('address_component').district
    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "get",
      }, res => {
        // console.log(res)
        this.setData({
          shoplist: res.Models
        })
        for (var a in this.data.shoplist) {
          this.setData({
            bnyr: this.data.shoplist[a].Bdate.slice(0, 10),
            enyr: this.data.shoplist[a].Edate.slice(0, 10)
          })
        }
      })
  },
  selecttab: function (e) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getone()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})